package com.axter.libs.photo.previewphoto;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.Toast;

import com.axter.libs.photo.R;
import com.axter.libs.photo.utils.PhotoUtils;
import com.axter.libs.photo.widget.HackyViewPager;

import java.util.ArrayList;
import java.util.Collections;

import pl.droidsonroids.gif.GifDrawable;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


/**
 * 查看图片详情
 *
 * @author axter
 */
@Deprecated
public class PreviewPhotoActivity extends Activity implements View.OnClickListener, PhotoViewAttacher.OnPhotoTapListener {
    public static final String EXTRA_IMAGE_PATHS = "image_paths";
    public static final String EXTRA_POSITION = "image_pos";
    public static final String EXTRA_TYPE = "image_type_del";
    public static final String EXTRA_OUT_DEL_POSITION = "image_del_position";
    public static final int EXTRA_TYPE_PREVIEW = 1;
    public static final int EXTRA_TYPE_DEL = 2;
    public static final int EXTRA_TYPE_CHOOSE = 3;
    /**
     * 处理默认,默认删除
     */
    protected int mType;
    private TextView tv_title;
    private View layout_top;
    private View layout_bottom;
    private HackyViewPager mViewPager;
    private PreviewPhotoAdapter mAdapter;
    private TranslateAnimation mShowTopAnimation;
    private TranslateAnimation mHideTopAnimation;
    private TranslateAnimation mShowBottomAnimation;
    private TranslateAnimation mHideBottomAnimation;
    /**
     * 当前page页坐标
     */
    private int mCurrentIndex;
    /**
     * 图片地址
     */
    private ArrayList<String> mPaths;
    /**
     * 图片地址坐标
     */
    private ArrayList<Integer> mPos;
    /**
     * 被删除图片地址坐标
     */
    private ArrayList<Integer> mDel;
    private TextView tv_page;
    /**
     * 工具栏是否处于显示状态
     */
    private boolean isShow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiveIntentArgs();

        setContentView(getContentView());

        initView();

        initData();
    }

    protected void receiveIntentArgs() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            mCurrentIndex = getIntent().getExtras().getInt(EXTRA_POSITION, 0);
            mPaths = getIntent().getExtras().getStringArrayList(EXTRA_IMAGE_PATHS);
            mType = getIntent().getExtras().getInt(EXTRA_TYPE, EXTRA_TYPE_PREVIEW);
        }

        if (mPaths == null || mPaths.size() == 0) {
            Toast.makeText(this, R.string.photoselect_no_pictures_to_preview, Toast.LENGTH_SHORT).show();
            super.finish();
            return;
        }

        mPos = new ArrayList<>(mPaths.size());
        mDel = new ArrayList<>(mPaths.size());
        for (int i = 0, count = mPaths.size(); i < count; i++) {
            mPos.add(i);
        }
    }

    protected int getContentView() {
        return R.layout.previewphoto;
    }

    protected void initView() {
        ViewStub viewstub_del = (ViewStub) findViewById(R.id.viewstub_del);
        ViewStub viewstub_preview = (ViewStub) findViewById(R.id.viewstub_preview);
        ViewStub viewstub_bottom = (ViewStub) findViewById(R.id.viewstub_bottom);
        if (mType == EXTRA_TYPE_DEL) {
            layout_top = viewstub_del.inflate();
            layout_top.findViewById(R.id.btn_left).setOnClickListener(this);
            layout_top.findViewById(R.id.btn_right).setOnClickListener(this);
            tv_title = (TextView) layout_top.findViewById(R.id.tv_title);
        } else if (mType == EXTRA_TYPE_CHOOSE) {
            viewstub_bottom.inflate();
            layout_bottom = findViewById(R.id.layout_bottom);
        } else if (mType == EXTRA_TYPE_PREVIEW) {
            viewstub_preview.inflate();
            tv_page = (TextView) findViewById(R.id.tv_page);
            if (mPaths.size() == 1) {
                tv_page.setVisibility(View.GONE);
            }
        }
        // 页面数据
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        mAdapter = new PreviewPhotoAdapter(this, mPaths);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCurrentIndex);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mCurrentIndex = position;
                setPageText(mCurrentIndex + 1);
            }

            @Override
            public void onPageSelected(int position) {
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).start();
                        }
                    }
                } catch (Exception ex) {

                }
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position - 1);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).stop();
                        }
                    }
                } catch (Exception ex) {

                }
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position + 1);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).stop();
                        }
                    }
                } catch (Exception ex) {

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        if (mType == EXTRA_TYPE_CHOOSE) {
            initBottomAnimation();
        } else if (mType == EXTRA_TYPE_DEL) {
            initTopAnimation();
        }
    }

    protected void initData() {
    }

    private void initTopAnimation() {
        // 从自已-1倍的位置移到自己原来的位置
        mShowTopAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        mShowTopAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                layout_top.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mHideTopAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f);
        mHideTopAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout_top.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mShowTopAnimation.setDuration(100);
        mHideTopAnimation.setDuration(100);
    }

    private void initBottomAnimation() {
        mShowBottomAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        mShowBottomAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                layout_bottom.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mHideBottomAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f);
        mHideBottomAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout_bottom.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mShowBottomAnimation.setDuration(100);
        mHideBottomAnimation.setDuration(100);
    }

    @Override
    public void onPhotoTap(View view, float v, float v1) {
        if (mType == EXTRA_TYPE_PREVIEW) {
            finish();
            return;
        }
        if (mType == EXTRA_TYPE_CHOOSE || mType == EXTRA_TYPE_DEL) {
            if (isShow) {
                isShow = false;
                layout_top.startAnimation(mHideTopAnimation);
                if (mType == EXTRA_TYPE_CHOOSE) {
                    layout_bottom.startAnimation(mHideBottomAnimation);
                }
            } else {
                isShow = true;
                layout_top.startAnimation(mShowTopAnimation);
                if (mType == EXTRA_TYPE_CHOOSE) {
                    layout_bottom.startAnimation(mShowBottomAnimation);
                }
            }
        }
    }

    @Override
    public void onOutsidePhotoTap() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_left) {
            finish();
        } else if (i == R.id.btn_right) {
            removePage();
        }
    }

    public void finish() {
        if (mType == EXTRA_TYPE_DEL && mDel != null && mDel.size() > 0) {
            Collections.sort(mDel, Collections.reverseOrder());
            Intent intent = new Intent();
            intent.putIntegerArrayListExtra(EXTRA_OUT_DEL_POSITION, mDel);
            setResult(Activity.RESULT_OK, intent);
            super.finish();
        } else {
            super.finish();
        }
    }

    protected void removePage() {
        if (mPaths.size() == 1) {
            mPaths.remove(0);
            mAdapter.notifyDataSetChanged();

            mDel.add(mPos.get(0));
            mPos.remove(0);

            finish();
        } else if (mCurrentIndex == mPaths.size() - 1) {
            int cur = mCurrentIndex;

            mAdapter.remove(cur);
            mDel.add(mPos.get(cur));
            mPos.remove(cur);

            if (mViewPager.getCurrentItem() == cur) {
                mViewPager.setCurrentItem(cur - 1, false);
            }
        } else {
            int cur = mCurrentIndex;

            mAdapter.remove(cur);
            mDel.add(mPos.get(cur));
            mPos.remove(cur);
        }
    }

    public void setPageText(int position) {
        if (tv_page != null) {
            tv_page.setText(PhotoUtils.toStrings(position, "/", mPaths.size()));
        } else if (tv_title != null) {
            tv_title.setText(PhotoUtils.toStrings(position, "/", mPaths.size()));
        }
    }

    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }

    public int getCount() {
        return mPaths.size();
    }

    @Override
    protected void onDestroy() {
        if (mAdapter != null) {
            mAdapter.loadingListener = null;
            mAdapter.progressListener = null;
            mAdapter.photoTapListener = null;
        }
        super.onDestroy();
    }
}
